package com.mall.controller;

import com.alibaba.fastjson.JSON;
import com.mall.bean.SystemPermission;
import com.mall.bean.SystemRolePermission;
import com.mall.service.SystemPermissionService;
import com.mall.service.SystemRolePermissionService;
import com.mall.vo.NodeVo;
import com.mall.vo.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * @PageName com.mall.controller
 * @Description 角色权限中间表控制层
 * @Author 何全裕
 * @Create 2017-09-07 22:00
 * @Version 1.0
 **/
@Controller
@RequestMapping("/role_permission")
public class SystemRolePermissionController {
    @Autowired
    private SystemRolePermissionService systemRolePermissionServiceImpl;

    @Autowired
    private SystemPermissionService systemPermissionService;

    @RequestMapping("/getSystemRolePermission")
    public ModelAndView getSystemRolePermission(String role_id){
        ModelAndView modelAndView = new ModelAndView("/system_role/SystemRolePermission");
        List<NodeVo> list = new ArrayList<NodeVo>();
        List<SystemPermission> all_list = systemPermissionService.getPermissionList();
        List<SystemRolePermission> role_list = systemRolePermissionServiceImpl.getSystemRolePermission(role_id);
        for(SystemPermission permission : all_list){
            boolean flag = false;
            NodeVo nodeVo = new NodeVo();
            nodeVo.setName(permission.getName());
            nodeVo.setOpen(true);
            nodeVo.setId(permission.getId().toString());
            if(permission.getParentid() == null || permission.getParentid().equals("") || permission.getParentid() == 1){  //一级菜单
                nodeVo.setPId("0");
            }else{
                nodeVo.setPId(permission.getParentid().toString());
            }
            for(SystemRolePermission rp : role_list){
                if((permission.getId()+"").equals(rp.getSysPermissionId())){
                    flag = true;
                    break;
                }
            }
            if(flag){
                nodeVo.setChecked(true);
            }else{
                nodeVo.setChecked(false);
            }
            list.add(nodeVo);
        }
        modelAndView.addObject("menus", JSON.toJSON(list));
        System.out.println(JSON.toJSON(list));
        return modelAndView;
    }

    @ResponseBody
    @RequestMapping("/deleteRolePermissionByRoleId")
    public Response deleteRolePermissionByRoleId(String role_id){
        systemRolePermissionServiceImpl.deleteRolePermissionByRoleId(role_id);
        return new Response("0","","");
    }

    @ResponseBody
    @RequestMapping("/insertRolePermission")
    public Response insertRolePermission(SystemRolePermission rolePermission,String menuIds){
        systemRolePermissionServiceImpl.deleteRolePermissionByRoleId(rolePermission.getSysRoleId());
        if(menuIds == null || menuIds.equals("")){
            return new Response("0","","");
        }
        String[] p_ids = menuIds.split(";");
        for(String pid : p_ids){
            rolePermission.setId(UUID.randomUUID().toString());
            rolePermission.setSysPermissionId(pid);
            systemRolePermissionServiceImpl.insertRolePermission(rolePermission);
        }
        return new Response("0","","");
    }

    @ResponseBody
    @RequestMapping("/deleteByPermissionId")
    public Response deleteByPermissionId(String permission_id){
        systemRolePermissionServiceImpl.deleteByPermissionId(permission_id);
        return new Response("0","","");
    }

}
